<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Dados extends Controller_GradeHorariaFrontEnd{
    public function before(){
        parent::logged_in_area();
    }
    public function action_index(){
            $this->view->body->content = Smarty_View::factory("2_insert_data/insert_data.tpl");

            $user_id = Model_User::get_user()->get_id();
            
            //carrega instituição
            $user_has_institution=Model_Institution::get_user_institution($user_id);
            if($user_has_institution){
                $this->view->body->content->institution = Smarty_View::factory("2_insert_data/existant_institution.tpl");
                $this->view->body->content->institution->institution_infos = new Model_Institution($user_has_institution);
            }else{
                $this->view->body->content->institution_form_tpl = Smarty_View::factory("2_insert_data/institution_form.tpl")->render();
            }
            
            //carrega matérias
            $courses = Model_Course::get_institution_courses($user_has_institution);
            foreach($courses as $i=>$course){
                $course_view = Smarty_View::factory("2_insert_data/existant_course.tpl");
                $course_view->course = $course;
                $course_view->course['add_new_class_view'] = Smarty_View::factory("2_insert_data/class_form.tpl");
                $course_view->course['add_new_class_view']->course_id = $course['id'];
                $course_view->course['add_new_class_view']->render();
                //carrega turmas
                foreach($course['classes'] as $j=>$class){
                    $existant_class = Smarty_View::factory("2_insert_data/existant_class.tpl");
                    $existant_class->class = $class;
                    $existant_class->render();
                    $course_view->course['classes'][$j] = $existant_class;
                }
                $courses[$i] = $course_view->render();
            }
            $this->view->body->content->class_time_interface = str_replace("\n", "", Smarty_View::factory("2_insert_data/insert_data_class_time.tpl")->render());
            $this->view->body->content->courses = $courses;
            $this->view->body->content->course_form = Smarty_View::factory("2_insert_data/course_form.tpl");
            $this->response->body($this->view);
        }
	
        
} 
